package com.leeyu.kafka.commit;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

// 默认都是消费后自动提交偏移量
public class ConsumerCommitBatch {

    public static void main(String[] args) {
        // 设置属性
        Properties properties = new Properties();
        // 连接地址
        properties.put("bootstrap.servers", "localhost:9092");
        // key value反序列化
        properties.put("key.deserializer", StringDeserializer.class);
        properties.put("value.deserializer", StringDeserializer.class);
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "groupA");

        // 从最早的消息开始消费，从头开始
        //properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        // 从已提交offset开始消费
        //properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        // 关闭自动提交偏移量
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);


        // 构建Kafka消费者对象
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);

        // 偏移量提交
        Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>();
        // 统计消费数量，计算提交时机
        AtomicInteger count = new AtomicInteger();
        try {
            // 订阅主题
            consumer.subscribe(Collections.singletonList("topic-g"));

            while (true) {
                try {
                    // 设置每秒从Kafka拉取消息
                    ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));

                    records.forEach(record -> {
                        String key = record.key();
                        String value = record.value();
                        int partition = record.partition();
                        System.out.println("消费消息成功：分区：" + partition + ", key：" + key + ", value：" + value);

                        offsets.put(new TopicPartition(record.topic(), partition),
                                new OffsetAndMetadata(record.offset() + 1, value));
                        // 累计消费数量
                        count.getAndIncrement();

                        // 每消费10条消息确认一次
                        if (count.get() % 10 == 0 ) {
                            consumer.commitAsync(offsets, new OffsetCommitCallback() {
                                @Override
                                public void onComplete(Map<TopicPartition, OffsetAndMetadata> map, Exception e) {
                                    if (e != null) {
                                        System.out.println("ACK确认提交异常！");
                                    }
                                }
                            });
                        }
                    });
                } catch (Exception e) {
                    System.out.println("消费消息异常！");
                    e.printStackTrace();
                }
            }
        } finally {
            try {
                // 同步提交确认，不会阻塞应用线程，并且会重试，确保成功
                consumer.commitSync(offsets);
            } finally {
                consumer.close();
            }
        }
    }
}
